//
//  QDWebViewController-Swift.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/18.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation


extension QDWebViewController {
    
    /// 点赞
    @objc func thumbup(id: String?){
        
        guard let id = id else {
            return;
        }
        
        articleAction(id: id, type: .favorite) {[weak self] (isActionSuccess) -> (Void) in
//            guard let strongSelf = self, isActionSuccess else { return }
        
           //调用js 点赞数 + 1
           self?.webView.evaluateJavaScript("thumbupCallback()", completionHandler: { (_, _) in })
        }
    
    }
    
    /// 收藏
    @objc func favorite(id: String?){

        guard let id = id else {
            return;
        }
        
        articleAction(id: id, type: .collect)//调用js
    }
    
    
    /// 分享
    @objc func shareAction(){
        guard let url = self.url else { return; }
        QDShareManager.share(url: "\(url)?share=1", imageUrl: shareImageUrl, title: shareTitle, desc: shareDesc, from: self)
    }
}


extension UIViewController {
    
    /// 对文章进行操作
    func articleAction(id: String, type: QDNetworkingManager.ArticleActionType,complete:((Bool) -> (Void))? = nil){
        
        /// 类型标题
        func typeTitle(type:  QDNetworkingManager.ArticleActionType) -> String {
            switch type {
            case .favorite: return "点赞"
            case .collect: return "收藏"
            }
        }
        
        checkLogin { //检验登陆
            //进行网络请求
            QDNetworkingManager.articleDidAction(articleId: id, type: type, complete: {(data,msg) in
                
                self.view.showTextHUD(text: msg, delayHidden: 0.5)
                
                /// 获得data
                guard let status = data["status"] as? Bool else {
                    complete?(false); return
                }
                
                ///
                complete?(status)
                
            }, fail: { (msg, type) in
                
                self.view.showTextHUD(text: msg)
            })
        }
    }
}


